home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 205_01 / fish.c < prev    next >
Text File  |  1980-01-01  |  12KB  |  478 lines

  1.  
  2. /*
  3. HEADER:                 CUG205.00;
  4. TITLE:                  GOFISH;
  5. DATE:                   09/24/86; 
  6. DESCRIPTION:            "Children's card game, based
  7.                         on CUG102 by Leor Zolman.";
  8. KEYWORDS:               games, cards;
  9. SYSTEM:                 MS-DOS;
  10. FILENAME:               GOFISH.C;
  11. WARNINGS:               "The author claims the copyright to this MS-DOS version
  12.                         and authorizes non-commercial use only.";
  13. AUTHORS:                 Michael M. Yokoyama;
  14. COMPILERS:              Microsoft;
  15. */
  16.  
  17. #include <conio.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <time.h>
  21.  
  22. /*  Throughout the program, `my' refers to the computer, `your' to the human */
  23.  
  24. #define CTYPE 13
  25. #define CTSIZ (CTYPE+1)
  26. #define DECK 52
  27. #define NOMORE 0
  28. #define DOUBTIT (-1);
  29.  
  30. /* data structures */
  31. int debug;
  32. void title();
  33. char myhand[CTSIZ];
  34. char yourhand[CTSIZ];
  35. char deck[DECK];
  36. int nextcd;
  37. int proflag = 2;
  38.  
  39. /* utility and output programs */
  40. shuffle()
  41. {
  42.   register i,j;
  43.   long iths;
  44.   time(&iths);
  45.   srand(iths);
  46.   for (i=0; i<DECK; ++i) deck[i] = (i%13)+1;  /* seed the deck */
  47.   for (i=DECK; i>0; --i){ /* select the next card at random */
  48.     deck[i-1] = choose(deck, i);
  49.   }
  50.   nextcd = 0;
  51. }
  52.  
  53. choose(a, n) char a[];
  54. {
  55.   /* pick and return one at random from the n choices in a */
  56.   /* The last one is moved to replace the one chosen */
  57.   register j, t;
  58.   if (n <= 0) error("Null choice");
  59.   j = rand() % n;
  60.   t = a[j];
  61.   a[j] = a[n-1];
  62.   return(t);
  63. }
  64.  
  65. draw() {
  66.   if (nextcd >= DECK) return(NOMORE);
  67.   return(deck[nextcd++]);
  68. }
  69.  
  70. error(s) char *s;
  71. {
  72.   fprintf(stderr, "Error:  ");
  73.   fprintf(stderr, s);
  74.   exit(1);
  75. }
  76.  
  77. empty(h) char  h[CTSIZ];
  78. {
  79.   register i;
  80.   for (i=1; i<=CTYPE; ++i){
  81.     if (h[i] != 0 && h[i] != 4) return(0);
  82.   }
  83.   return(i);
  84. }
  85.  
  86. mark(hand,cd) char hand[CTSIZ];
  87. {
  88.   if (cd != NOMORE){
  89.     ++hand[cd];
  90.     if (hand[cd] > 4){
  91.       error("Mark overflow");
  92.     }
  93.   }
  94.   return(cd);
  95. }
  96.  
  97. deal(hand, n) char hand[CTSIZ];
  98. {
  99.   while (n--){
  100.     if (mark(hand, draw()) == NOMORE) error("Deck exhausted");
  101.   }
  102. }
  103.  
  104. char *cname[14];
  105. stats(){
  106.   register i, ct, b;
  107.   if (proflag) printf("Professional level\n");
  108.   b = ct = 0;
  109.   for (i=1; i<=CTYPE; ++i){
  110.     if (myhand[i] == 4) ++b;
  111.     else ct += myhand[i];
  112.   }
  113.   if (b){
  114.     printf("My books:  ");
  115.     for (i=1; i<=CTYPE; ++i){
  116.       if (myhand[i] == 4) printf("%s ", cname[i]);
  117.     }
  118.     printf("\n");
  119.   }
  120.   printf("%d cards in my hand, %d in the pool\n", ct, DECK-nextcd);
  121.   printf("You ask me for:  ");
  122. }
  123.  
  124. phand(h) char h[CTSIZ];
  125. {
  126.   register i, j;
  127.   j = 0;
  128.   for (i = 1; i<= CTYPE; ++i){
  129.     if (h[i] == 4) {
  130.       ++j;
  131.       continue;
  132.     }
  133.     if (h[i]){
  134.       register k;
  135.       k = h[i];
  136.       while (k--) printf("%s ", cname[i]);
  137.     }
  138.   }
  139.   if (j){
  140.     printf("+ Books of ");
  141.     for (i=1; i<=CTYPE; ++i){
  142.       if (h[i] == 4) printf("%s ", cname[i]);
  143.     }
  144.   }
  145.   printf("\n");
  146. }
  147.  
  148. main(argc, argv) char * argv[];
  149. {
  150.   long iths;
  151.   /* initialize shuffling, ask for instructions, play game, die */
  152.   register c;
  153.   cname[0]   = "Nomore!!!";
  154.   cname[1]   = "A";
  155.   cname[2]   = "2";
  156.   cname[3]   = "3";
  157.   cname[4]   = "4";
  158.   cname[5]   = "5";
  159.   cname[6]   = "6";
  160.   cname[7]   = "7";
  161.   cname[8]   = "8";
  162.   cname[9]   = "9";
  163.   cname[10]  ="10";
  164.   cname[11]  = "J";
  165.   cname[12]  = "Q";
  166.   cname[13]  = "K";
  167.   if (argc > 1 && argv[1][0] == '-') debug++;
  168.   time(&iths);
  169.   srand(iths);
  170.   title();
  171.   printf("Do you want the rules? (y/n)");
  172.   if ((c=tolower(getche())) == 'y')
  173.     instruct();
  174.   printf("%c[2J",27);
  175. start:
  176.   game();
  177.   exit(0);
  178. }
  179.  
  180. /*      print instructions */
  181. instruct()
  182. {
  183.   int c;
  184.   printf("%c[2J                                G O   F I S H\n\n\n",27);
  185.   printf("     GO FISH is a card game for children.\n\n");
  186.   printf("     The object is to accumulate `books' of cards, a `book' being a \n");
  187.   printf("     set of four cards of a kind such as four Jacks, four Aces, etc.\n\n");
  188.   printf("     Players alternate turns.  The game starts with each player being \n");
  189.   printf("     dealt seven cards.  The rest of the cards are placed in a `pond'.\n\n");
  190.   printf("     After looking at the cards in his hand, a player asks his opponent \n");
  191.   printf("     for any cards of a particular number . . . For example, he might \n");
  192.   printf("     ask \"Do you have any Fives?\"\n\n");
  193.   printf("     If the opponent has one or more cards of the card requested, he \n");
  194.   printf("     must give them to the first player.  If he does not have the card \n");
  195.   printf("     requested, he says `Go fish'.\n\n");
  196.   printf("     The first player then draws a card from the `pond' of undealt cards.  \n");
  197.   printf("     If he draws the card he had last requested, he draws again.  If \n");
  198.   printf("     the card is not the one requested, the next player takes a turn.\n\n\n");
  199.   printf("Press any key to see the next screen . . .");
  200.   c = getche();
  201.   printf("%c[2J\n     Once a player makes a book, no further action takes place with that \n",27);
  202.   printf("     face value.\n\n");
  203.   printf("     PLAYING AGAINST THE COMPUTER:\n\n");
  204.   printf("     To play GO FISH on the computer, simply make guesses by typing\n\n");
  205.   printf("          a, 2, 3, 4, 5, 6, 7, 8, 9, 10, j, q, or k.\n\n");
  206.   printf("     Hit return to get information about the size of my hand, my books,\n");
  207.   printf("     and the size of the pool.\n\n");
  208.   printf("          G O O D   L U C K !\n\n\n\n\n\n\n\n\n");
  209.   printf("Press any key to start the game . . .\n");
  210.   c = getche();
  211.   printf("%c[2J",27);
  212. }
  213.  
  214. /* play game */
  215. game()
  216. {
  217.   int i,j;
  218.   shuffle();
  219.   for (i = 0; i < CTSIZ; i++) {
  220.     myhand[i] = 0;
  221.     yourhand[i] = 0;
  222.   }
  223.   deal(myhand, 7);
  224.   deal(yourhand, 7);
  225.   start(myhand);
  226.   while (1) {
  227.     register g;
  228.     /* you make repeated guesses */
  229.     while (1) {
  230.       printf("\nYour hand is:  ");
  231.       phand(yourhand);
  232.       printf("You ask me for:  ");
  233.       if (!move(yourhand, myhand, g=guess(), 0)) break;
  234.       printf("Guess again\n");
  235.     }
  236.     /* I make repeated guesses */
  237.     while (1) {
  238.       if ((g=myguess()) != NOMORE){
  239.         printf("I ask you for:  %s\n", cname[g]);
  240.       }
  241.       if (!move(myhand, yourhand, g, 1)) break;
  242.       printf("I get another guess\n");
  243.     }
  244.   }
  245. }
  246.  
  247. /*      reflect the effect of a move on the hands */
  248. move(hs, ht, g, v) char hs[CTSIZ], ht[CTSIZ];
  249. {
  250.   /* hand hs has made a guess, g, directed towards ht */
  251.   /* v on indicates that the guess was made by the machine */
  252.   register d;
  253.   char *sp, *tp;
  254.   sp = tp = "I";
  255.   if (v) tp = "You";
  256.   else sp = "You";
  257.   if (g == NOMORE){
  258.     d = draw();
  259.     if (d == NOMORE) score();
  260.     else {
  261.       printf("No Cards\n");
  262.       if (!v) printf("You draw %s\n", cname[d]);
  263.       mark(hs, d);
  264.     }
  265.     return(0);
  266.   }
  267.   if (!v) heguessed(g);
  268.   if (hs[g] == 0){
  269.     if (v) error("Cheat!!!  ");
  270.     printf("You don't have any %s's\n", cname[g]);
  271.     return(1);
  272.   }
  273.   if (ht[g]){ /* successful guess */
  274.     printf("%s have ",tp);
  275.     switch(ht[g]) {
  276.     case 1 :
  277.       printf("one ");
  278.       break;
  279.     case 2 :
  280.       printf("two ");
  281.       break;
  282.     case 3 :
  283.       printf("three ");
  284.       break;
  285.     case 4 :
  286.       printf("four ");
  287.       break;
  288.     }
  289.     printf("%s%s\n",cname[g], ht[g] > 1 ? "'s" : "");
  290.     hs[g] += ht[g];
  291.     ht[g] = 0;
  292.     if (hs[g] == 4) madebook(g);
  293.     return(1);
  294.   }
  295.   /* GO FISH! */
  296.   printf("%s say \"Go fish!\"\n\n", tp);
  297. newdraw:
  298.   d = draw();
  299.   if (d == NOMORE) {
  300.     printf("No more cards\n");
  301.     return(0);
  302.   }
  303.   mark(hs, d);
  304.   if (!v) printf("You draw %s\n", cname[d]);
  305.   if (hs[d] == 4) madebook(d);
  306.   if (d == g){
  307.     printf("%s drew the guess\n%s get another draw\n", sp,sp);
  308.     if (!v) hedrew(d);
  309.     goto newdraw;
  310.   }
  311.   return(0);
  312. }
  313.  
  314. madebook(x){
  315.   printf("Made a book of %s's\n", cname[x]);
  316. }
  317. score(){
  318.   register my, your, i;
  319.   my = your = 0;
  320.   printf("\nGame over\n\nMy books:   ");
  321.   for (i=1; i<=CTYPE;++i){
  322.     if (myhand[i] == 4){
  323.       ++my;
  324.       printf("%s ", cname[i]);
  325.